package com.wood_sheep.Model.Dao;

import com.wood_sheep.Model.Entity.Login;

import java.sql.*;

/*
模型层处理登录
 */
public class LoginDao {


    public static int login(Login login) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql;
        int result = -1;
        int flag = -1;//-1:系统异常 0：用户名密码有误 1：登录成功
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");
            sql = "select count (*) from login where uname =? And upwd =?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, login.getUname());
            preparedStatement.setString(2, login.getUpwd());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                result = resultSet.getInt(1);
            }
            if (result > 0) {
                flag = 1;
            } else {
                flag = 0;
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            flag = -1;
        } catch (SQLException e) {
            e.printStackTrace();
            flag = -1;
        } catch (Exception e) {
            e.printStackTrace();
            flag = -1;

        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                    preparedStatement.close();
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return flag;
    }
}
